package com.star.boxiaotong.controller;


import com.alibaba.excel.EasyExcel;
import com.star.boxiaotong.common.BaseResponse;
import com.star.boxiaotong.common.ErrorCode;
import com.star.boxiaotong.common.ResultUtils;
import com.star.boxiaotong.exception.BusinessException;
import com.star.boxiaotong.module.ColumnEnum;
import com.star.boxiaotong.module.request.AddTagsRequest;
import com.star.boxiaotong.module.request.ExtractSingleRequest;
import com.star.boxiaotong.module.request.GetLowPriceRequest;
import com.star.boxiaotong.module.request.UploadRequest;
import com.star.boxiaotong.once.DataInfo;
import com.star.boxiaotong.service.GoodsService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;


@Controller
@RequestMapping("/goods")
public class GoodsController {


    @Resource
    private GoodsService goodsService;


    @ResponseBody
    @PostMapping("/addTags")
    public BaseResponse<String> brandNameTags (@RequestBody AddTagsRequest addTagsRequest) {
        if (addTagsRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String result = goodsService.addBrandTags(addTagsRequest);

        return ResultUtils.success(result);
    }


    @ResponseBody
    @PostMapping("/extract")
    public BaseResponse<Double> extractSingle(@RequestBody ExtractSingleRequest extractSingleRequest) {
        if (extractSingleRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        String goods_name = extractSingleRequest.getGoods_name();
        if (StringUtils.isBlank(goods_name)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "商品描述为空");
        }
        Double result = goodsService.extractNumber(goods_name);

        return ResultUtils.success(result);

    }

    @ResponseBody
    @PostMapping("/getLowPrice")
    public BaseResponse<Double> getLowPrice(@RequestBody GetLowPriceRequest getLowPriceRequest) {
        if (getLowPriceRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        Double result = goodsService.getLowPrice(getLowPriceRequest);
        return ResultUtils.success(result);

    }


    @ResponseBody
    @PostMapping("/upload")
    public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file, String text) {

        String localFilePath = null;
        try{
            File tempFile = File.createTempFile("excel", ".temp");
            file.transferTo(tempFile);
             localFilePath = tempFile.getAbsolutePath();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        String text = uploadRequest.getText();

        ColumnEnum enumByValue = ColumnEnum.getEnumByValue(text);
        String column = enumByValue.getColumn();

        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 同步读取会自动finish
        List<DataInfo> dataInfoList =
                EasyExcel.read(localFilePath).head(DataInfo.class).sheet().doReadSync();
        System.out.println("总数 = " + dataInfoList.size());
        Map<String, List<DataInfo>> listMap =
                dataInfoList.stream()
                        .collect(Collectors.groupingBy(dataInfo -> {
                            Method method = null;
                            Class<? extends DataInfo> c = dataInfo.getClass();
                            try {
                                method = c.getMethod("get" + column);
                            } catch (NoSuchMethodException e) {
                                e.printStackTrace();
                            }
                            try {
                                return (String)method.invoke(dataInfo);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            return dataInfo.getArea();
                        }));

        for (Map.Entry<String, List<DataInfo>> stringListEntry : listMap.entrySet()) {
            if (stringListEntry.getValue().size() > 0) {
                String importFile = "D:\\Ideaproject\\boxiaotong\\src\\main\\resources\\dataTest"+ stringListEntry.getKey()+".xlsx";
                EasyExcel.write(importFile, DataInfo.class).sheet("").doWrite(stringListEntry.getValue());

            }
        }
        System.out.println("不重复昵称数 = " + listMap.keySet().size());

        return null;

    }










}
